objective of this section: exec replaces the process image of the EXEC Association function Group (EXECL, EXECLP, Execle, EXECV, EXECVP) One, exec replaces the process image on the process creation Unix takes a unique approach, It separates the process creation from the loading of a new process image. The advantage is that there is more room to manage the two operations. When we create a process, we usually replace the child process with a new process
Run execl, execle, execlp, execv, execve, and execvp functions in the process, execleexecvpAbstract:This article describes how to run new code in the process and the basic usage of exec functions.
Run new code in the processAfter using the fork function to create a sub-process, if you want to run a new program in the current sub-process, you can call the exec function to execute another program. when a process calls the exec function, the user space r
5 process primitives: execl (), execlp (), execle (), execv (), execvp (), execvp (), execve (), execlpexecleZookeeper
Header files dependent on the 1.exe c family
# Include
Extern char ** environ;
2 function declaration
// The first parameter is the absolute location of the executable program.
// The second parameter: The parameter required when the executable program runs. It is a variable parameter, separated by commas (,).
// When the parameter e
C language Execv () function: Performing file functionsheader file:
#include
To define a function:
int EXECV (const char * path, char * const argv[]);
Function Description: The EXECV () is used to execute the path of the file represented by the parameter path string, unlike Execl () where the Execve () takes only two arguments, and th
system using the Pstree command:650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M02/87/3D/wKioL1fY0hfxn839AAC1J8Y8Y9g510.png-wh_500x0-wm_3 -wmp_4-s_89145685.png "title=" Qq20160914122852.png "alt=" Wkiol1fy0hfxn839aac1j8y8y9g510.png-wh_50 "/>4. Create a processThe creation of a process is done by 2 system function families.(1), Execv function familyint execl (const char *path, const char *arg, ...); int EXECLP (const char *file, const char *ar
Linux schedules the operation of a process by maintaining five states. These five states are: Run, interruptible, non-interruptible, zombie, stop.PID to identify different processes, each of the processes in Linux has a unique process number.A PCB block is a process resource1.fork functionInclude pid_t fork (void)return value 0: Child process child process ID (greater than 0)-1: ErrorThe child process that is obtained by using the fork function is a replica of the parent process. Using the fork
Execl (Execution file)
Related functions
Fork, execle, execlp, execv, execve, execvp
Header file
# Include
Define functions
Int execl (const char * path, const char * Arg ,....);
Function Description
Execl () is used to execute the file path represented by the path string parameter. The following parameter indicates that the previous argv (0), argv [1]…, The last parameter must end with a null pointer.
in the process space of the parent-child process, and the child processes that are accessible to the parents process can also.Next, let's take a look at a few examples of what Linux is namespace.UTS NamespaceThe following code, I omitted the above header files and data structure definition, only the most important part.int Container_main (void* arg){printf ("Container-inside the container!\n");SetHostName ("container", 10); /* Set hostname * *EXECV (
read this series of articles from the very beginning, but there is still a big question: Since all new processes are generated by fork, in addition, the sub-processes generated by fork are almost identical with the parent process. Doesn't that mean that all the processes in the system should be identical? Moreover, in our common sense, when we execute a program, the content of the new process should be the content of the program. Did we get it wrong? Obviously not. To solve these questions, we
will be modified based on this program ):# Define _ GNU_SOURCE # include/* defines a stack for clone. The stack size is 1 MB */# define STACK_SIZE (1024*1024) static char container_stack [STACK_SIZE]; char * const container_args [] = {"/bin/bash", NULL}; int container_main (void * arg) {printf ("Container-inside the container! /N ");/* execute a shell directly to check whether the resources in the process space are isolated */execv (container_args [0
In Linux, there is no exec () function, exec refers to a set of functions, a total of 6, namely: #include finds the executable file according to the specified file name and replaces the contents of the calling process with it, in other words, executing an executable file inside the calling process。 The executable file here can be either aBinary FilesOr it can be executed under any Linuxscript File。 Relationship between function name and parameter: Take a closer look, these 6 functions are all st
, use the fork system to call and create a sub-process. 2) use the exec function in the sub-process, execute the program that needs to be automatically restarted. 3) execute wait in the parent process to wait for the completion of the Child process, and then create a new child process.
Usage:
#./Portmap the path of the program to be monitored # parameters required by ARGs Portmap$./Supervisor./Portmap ARGs .....
The Code is as follows:
/* * ** Supervisor ** Author: liyangguang (liyan
is a child process? This is the function of the child process number, the kernel returns the child process number to the parent process, but returns to the child process is 0, so in this way, the parent-child process logic is diverted.Where the process code, data, and stack come fromWhere does the data from the process come from? That is the function of the EXECV function, in fact, the shell code is not complex, just the user's input is separated by
, (later, our program will make changes based on this program):#define _gnu_source#include #include #include #include #include #include/* Define a stack for clone, stack size 1M */#define STACK_SIZE (10 * 1024x768) static char container_stack[stack_size];char* const container_args[] = { "/bin/bash", null};int Container_main (void* Arg) { printf ("Container-inside the container!/n"); /* Execute a shell directly so we can see if the resources in the process space are quarantined /
) getgid ()); /* Wait for the parent process to notify and then go down (synchronization between processes) */charch; Close (pipefd[1]); Read (Pipefd[0], ch, 1); printf ("Container [%5d]–setup hostname!/n", Getpid ()); Set hostname sethostname ("container", 10); Remount "/proc" to make sure the "top" and "PS" Show container ' s Information mount ("proc", "/proc", "Proc", 0, NULL); EXECV (Container_args[0], Container_args); prin
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.